﻿using Architecture.Domain.Dto;
using Architecture.Domain.RBAC;
using Architecture.Repository.Interface;
using Architecture.Service.Impl;
using Architecture.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Service
{
    public class LeaveService:ILeaveService
    {
        protected readonly ILeaveRepository _leaveRepository;
        protected readonly IUserRepository _userRepository;

        public LeaveService(ILeaveRepository leaveRepository, IUserRepository userRepository)
        {
            _leaveRepository = leaveRepository;
            _userRepository = userRepository;
        }

        /// <summary>
        /// 部门领导
        /// </summary>
        /// <param name="sectorid"></param>
        /// <returns></returns>
        public async Task<List<UserInfo>> Leading(int sectorid)
        {
            var userList=await _userRepository.GetAllAsync();
            var list=(from u in userList where u.SectoriD==sectorid
                      && u.Leading==1 select new UserInfo
                      {
                          UserName = u.UserName,
                      }).ToList();
            return list;
        }

        /// <summary>
        /// 请假表添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> LeaveAdd(Leaved model)
        {
            model.AddName = "张三";
            model.AddTime = DateTime.Now;
            return await _leaveRepository.AddAsync(model);
        }
        /// <summary>
        /// 请假列表
        /// </summary>
        /// <returns></returns>
        public async Task<PageListDto<Leaved>> GetLeading(int pageIndex, int pageSize, string? userName)
        {
            PageListDto<Leaved> page = new PageListDto<Leaved>();

            var list =(from l in await _leaveRepository.GetAllAsync() join u in 
                       await _userRepository.GetAllAsync() on l.Sectorid equals u.SectoriD where u.Leading==1 
                       select new Leaved
                       {
                           LeaveId = l.LeaveId,
                           UserName = l.UserName,
                           Sectorid = l.Sectorid,
                           LeaveType = l.LeaveType,
                           LeaveTime = l.LeaveTime,
                           TotaWorkTime = l.TotaWorkTime,
                           ReportedTime = l.ReportedTime,
                           LeaveState = l.LeaveState,
                           LeaveReason = l.LeaveReason,
                           DepartmentHeader = l.DepartmentHeader,
                           AddTime = l.AddTime,
                       }).ToList();
            if (userName != null)
            {
                list=list.Where(x=>x.UserName.Contains(userName)).ToList();
            }
            page.ListCount = list.Count;
            page.PageCount = (int)Math.Ceiling((page.ListCount * 1.0) / pageSize);
            page.Lists =list.OrderByDescending(x=>x.LeaveId).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return page;
        }
        /// <summary>
        /// 请假真删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> LeaveDelete(int id)
        {
            var list =await _leaveRepository.isDelte(id);
            return list;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Leaved> GetLeaved(int id)
        {
            var list = await _leaveRepository.GetAsync(id);
            return list;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> UpdateLeave(Leaved model)
        {
            var leavelist=await _leaveRepository.GetAsync(model.LeaveId);
            leavelist.UserName = model.UserName;
            leavelist.Sectorid = model.Sectorid;
            leavelist.LeaveType = model.LeaveType;
            leavelist.LeaveTime = model.LeaveTime;
            leavelist.ReportedTime = model.ReportedTime;
            leavelist.TotaWorkTime = model.TotaWorkTime;
            leavelist.LeaveReason = model.LeaveReason;
            leavelist.DepartmentHeader = model.DepartmentHeader;
            return await _leaveRepository.UpdateAsync(leavelist);
        }
        /// <summary>
        /// 审批状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> VettingLeave(Leaved model)
        {
            var leavelist = await _leaveRepository.GetAsync(model.LeaveId);
            leavelist.LeaveState = model.LeaveState;
            return await _leaveRepository.UpdateAsync(leavelist);
        }

    }
}
